﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class Admin_decochange_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();

                }
            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlComList.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlComList.DataBind();
            this.ddlBuildList.DataSource = dal.GetBuild(int.Parse(this.ddlComList.SelectedValue), null, true);
            this.ddlBuildList.DataBind();
            this.ddlSelfUnitList.DataSource = dal.GetUnit(int.Parse(this.ddlComList.SelectedValue), int.Parse(this.ddlBuildList.SelectedValue), null);
            this.ddlSelfUnitList.DataBind();
        }


        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.Del_decorated(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {

                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.Get_decorated(int.Parse(this.hidId.Value), null);
                if (dt.Rows.Count > 0)
                {
                    this.ddlComList.Items.FindByValue(dt.Rows[0]["Self_Community_Code"].ToString()).Selected = true;
                    this.ddlBuildList.Items.FindByValue(dt.Rows[0]["Self_Build_Code"].ToString()).Selected = true;
                    this.ddlSelfUnitList.Items.FindByValue(dt.Rows[0]["Self_Unit_Code"].ToString()).Selected = true;
                    this.ddlU_Self_No.Items.FindByValue(dt.Rows[0]["U_Self_No"].ToString()).Selected = true;
                    this.txtSelf_Name.Text = dt.Rows[0]["Req_Name"].ToString();//通过U_Self_No取出业主姓名
                    this.txtDecoNo.Text = dt.Rows[0]["DecoNo"].ToString();
                    this.txtReq_Date.Text = dt.Rows[0]["Req_Date"].ToString();
                    this.txtReq_Name.Text = dt.Rows[0]["Req_Name"].ToString();
                    this.ddlReq_Class.Items.FindByValue(dt.Rows[0]["Req_Class"].ToString()).Selected = true;
                    this.ddlReq_Project.Items.FindByValue(dt.Rows[0]["Req_Project"].ToString()).Selected = true;
                    this.ddlReq_Key_Word.Items.FindByValue(dt.Rows[0]["Req_Key_Word"].ToString()).Selected = true;
                    this.ddlReq_Results.Items.FindByValue(dt.Rows[0]["Req_Results"].ToString()).Selected = true;
                    this.txtResults_Man.Text = dt.Rows[0]["Results_Man"].ToString();
                }
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string Self_Community = this.ddlComList.SelectedValue;
            string BuildList = this.ddlBuildList.SelectedValue;
            string SelfUnitList = this.ddlSelfUnitList.SelectedValue;
            string U_Self_No = this.ddlU_Self_No.SelectedValue; 
            string Req_Name = this.txtReq_Name.Text.Trim();
            string DecoNo = this.txtDecoNo.Text.Trim();
            string Req_Date = this.txtReq_Date.Text.Trim();
            string Req_Remark = this.txtReq_Remark.Text.Trim();
            string Req_Class = this.ddlReq_Class.SelectedValue;
            string Req_Project = this.ddlReq_Project.SelectedValue;
            string Req_Key_Word = this.ddlReq_Key_Word.SelectedValue;
            string Req_Results = this.ddlReq_Results.SelectedValue;
            string Results_Man = this.txtResults_Man.Text.Trim(); 

            DateTime Checkdate;
            if (!DateTime.TryParse(Req_Date, out Checkdate)) { PM.Web.Common.JsAlert(this, "请输入正确的申请变更日期!"); return; }  

            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.Add_face_m_unit_deco_change(int.Parse(Self_Community), int.Parse(BuildList), int.Parse(BuildList), int.Parse(U_Self_No), int.Parse(DecoNo), DateTime.Parse(Req_Date), Req_Name, Req_Remark,
                                            Req_Class, Req_Project, Req_Key_Word, Req_Results, Results_Man)) ;
                {
                    PM.Web.Common.JsAlert(this, "保存成功！", true);
                }
            }
            else
            {
                if (dal.Modify_face_m_unit_deco_change(int.Parse(hidId.Value), int.Parse(Self_Community), int.Parse(BuildList), int.Parse(BuildList), int.Parse(U_Self_No), int.Parse(DecoNo), DateTime.Parse(Req_Date), Req_Name, Req_Remark,
                                            Req_Class, Req_Project, Req_Key_Word, Req_Results, Results_Man)) ;
                {
                    PM.Web.Common.JsAlert(this, "修改成功！", true);
                }
            }
        }

    }
}